package com.br.ufrn.laut.braipetro.app.autenticacao;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.br.ufrn.laut.braipetro.app.ConfiguracaoActivity;
import com.br.ufrn.laut.braipetro.app.PrincipalActivity;
import com.br.ufrn.laut.braipetro.app.R;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class LoginActivity extends Activity {

    private EditText edtUser = null;
    private EditText edtPassword = null;
    private TextView registerScreen = null;
    private Button btnLogin;

    Animation animAlpha = null;
    final Context context = this;

    private ConfiguracaoActivity configuracaoActivity = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        edtUser = (EditText) findViewById(R.id.edtUser);
        edtPassword = (EditText) findViewById(R.id.edtPassword);
        registerScreen = (TextView) findViewById(R.id.txvRegistrar);
        btnLogin = (Button) findViewById(R.id.btnLogin);

        animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);

        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });

        configuracaoActivity = new ConfiguracaoActivity(getApplicationContext());
    }

    @Override
    public void onResume() {
        super.onResume();
        try {
            if (configuracaoActivity.conectado())
                btnLogin.setEnabled(true);
            else
                btnLogin.setEnabled(false);
        }
        catch (Exception e) {
            System.out.print("Erro");
        }
    }

    public void login(View view) {

        btnLogin.startAnimation(animAlpha);

        if (edtUser.getText().toString().equals("admin") &&
                edtPassword.getText().toString().equals("admin")) {
            Toast.makeText(getApplicationContext(), "Conectando...",
                    Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, PrincipalActivity.class);
            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(), "Usuário / Senha incorretos",
                    Toast.LENGTH_SHORT).show();

        }
    }

    public void configuracao(View view) {
        Intent intent = new Intent(context, ConfiguracaoActivity.class);
        startActivity(intent);

    }
}